package wellplatescanner;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;

import wellplatescanner.ScannerDevice.ScannerCapabilities;
import wellplatescanner.ScannerDevice.ScannerSettings;

public class ProjectComposite extends Composite {

	private Text txtProjectPath;
	private Text outPath;
	private Text txtScanner_1;
	private Text txtLightPath;
	private Text txtLightSource;
	private Text txtLampState;
	private Text txtBrightness;
	private Text txtGamma;
	private Text txtExposure;
	private Text txtContrast;
	private Text resolution;
	private Combo lightPath;	
	private Combo lightSource;	
	private Combo lampState; 
	private Slider gamma;
	private Combo brightness;
	private Combo contrast;
	private Combo exposureTime;
	private Text txtScanFrequency;
	private Text text;

	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ProjectComposite(final Shell parent, int style) {
		super(parent, style);
		
		outPath = new Text(this, SWT.BORDER);
		outPath.setEditable(false);
		outPath.setBounds(84, 12, 202, 19);
		Button btnSetOutPath = new Button(this, SWT.NONE);
		btnSetOutPath.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = outPath.getText();
				DirectoryDialog dlg = new DirectoryDialog(parent);
				dlg.setFilterPath(path);
				path = dlg.open();
			    outPath.setText(path);
			}
		});
		btnSetOutPath.setBounds(292, 10, 36, 23);
		btnSetOutPath.setText("...");

		txtProjectPath = new Text(this, SWT.READ_ONLY);
		txtProjectPath.setText("Project path");
		txtProjectPath.setBounds(10, 15, 68, 19);

		txtScanner_1 = new Text(this, SWT.READ_ONLY);
		txtScanner_1.setText("Resolution");
		txtScanner_1.setBounds(10, 57, 68, 19);
		
		resolution = new Text(this, SWT.BORDER);
		resolution.setBounds(91, 54, 76, 19);
		
		txtLightPath = new Text(this, SWT.READ_ONLY);
		txtLightPath.setText("Light Path");
		txtLightPath.setBounds(12, 110, 68, 19);
		
		lightPath = new Combo(this, SWT.NONE);
		lightPath.setBounds(91, 107, 93, 21);
		
		txtLightSource = new Text(this, SWT.READ_ONLY);
		txtLightSource.setText("Light Source");
		txtLightSource.setBounds(10, 137, 68, 19);
		
		lightSource = new Combo(this, SWT.NONE);
		lightSource.setBounds(91, 134, 93, 21);
		
		txtLampState = new Text(this, SWT.READ_ONLY);
		txtLampState.setText("Lamp State");
		txtLampState.setBounds(10, 87, 68, 19);
		
		lampState = new Combo(this, SWT.NONE);
		lampState.setBounds(91, 80, 93, 21);
		
		txtGamma = new Text(this, SWT.READ_ONLY);
		txtGamma.setText("Gamma");
		txtGamma.setBounds(204, 55, 47, 19);
		
		gamma = new Slider(this, SWT.NONE);
		gamma.setPageIncrement(1);
		gamma.setBounds(269, 56, 103, 17);
		
		txtBrightness = new Text(this, SWT.READ_ONLY);
		txtBrightness.setText("Brightness");
		txtBrightness.setBounds(205, 87, 68, 19);
		
		brightness = new Combo(this, SWT.NONE);
		brightness.setBounds(279, 84, 93, 21);
		
		txtContrast = new Text(this, SWT.READ_ONLY);
		txtContrast.setText("Contrast");
		txtContrast.setBounds(205, 110, 68, 19);
		
		contrast = new Combo(this, SWT.NONE);
		contrast.setBounds(279, 107, 93, 21);
		
		txtExposure = new Text(this, SWT.READ_ONLY);
		txtExposure.setText("Exposure Time");
		txtExposure.setBounds(203, 137, 76, 19);
		
		exposureTime = new Combo(this, SWT.NONE);
		exposureTime.setBounds(279, 134, 93, 21);
		
		txtScanFrequency = new Text(this, SWT.READ_ONLY);
		txtScanFrequency.setBounds(12, 162, 58, 19);
		txtScanFrequency.setText("Scan delay");
		
		text = new Text(this, SWT.BORDER);
		text.setBounds(93, 162, 76, 19);


	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	public void updateFromProject(Project project, ScannerCapabilities cap) {
		outPath.setText(project.path);
		text.setText(Float.toString(project.delay));
		setupScannerInterface(project.scannerSettings, cap);
	}

	public void updateProject(Project project) {
		project.path = outPath.getText();
		project.delay = Float.parseFloat(text.getText());
		getScannerSettings(project.scannerSettings);
	}
	
	private void setupScannerInterface(ScannerSettings settings, ScannerCapabilities cap) {
		
		resolution.setText(Double.toString(settings.resolution));
		
		setupCombo(lampState, cap.supportedLampState, settings.lampState);
		setupCombo(lightPath, cap.supportedLightPath, settings.lightPath);
		setupCombo(lightSource, cap.supportedLightSource, settings.lightSource);
		
		gamma.setSelection((int)(100*settings.gamma));
				
		setupCombo(brightness, cap.supportedBrightness, settings.brightness);
		setupCombo(contrast, cap.supportedContrast, settings.contrast);
		setupCombo(exposureTime, cap.supportedExposureTime, settings.exposureTime);
	}
	
	public void getScannerSettings(ScannerDevice.ScannerSettings settings) {
		
		settings.resolution = Double.parseDouble(resolution.getText());
		
		if(lampState.isEnabled()) {
			settings.lampState = Boolean.parseBoolean(lampState.getItem(lampState.getSelectionIndex()));
		}
		if(lightPath.isEnabled()) {
			settings.lightPath = Integer.parseInt(lightPath.getItem(lightPath.getSelectionIndex()));
		}
		if(lightSource.isEnabled()) {
			settings.lightSource = Integer.parseInt(lightSource.getItem(lightSource.getSelectionIndex()));
		}
		
		settings.gamma = ((double)gamma.getSelection())/100.0;
		
		if(brightness.isEnabled()) {
			settings.brightness = Double.parseDouble(brightness.getItem(brightness.getSelectionIndex()));
		}
		
		if(contrast.isEnabled()) {
			settings.contrast = Double.parseDouble(contrast.getItem(contrast.getSelectionIndex()));
		}

		if(exposureTime.isEnabled()) {
			settings.exposureTime = Double.parseDouble(exposureTime.getItem(exposureTime.getSelectionIndex()));
		}
	}

	private void setupCombo(Combo combo, int[] elements, int selectedValue) {
		
		combo.removeAll();
		
		if(elements.length == 0) {
			combo.setEnabled(false);
			return;
		}

		combo.setEnabled(true);
		int currentElementIndex = -1;
		for(int i=0; i<elements.length; i ++) {
			int element = elements[i];
			if(element == selectedValue) {
				currentElementIndex = i;
			}
			combo.add(Integer.toString(element));
		}
		if(currentElementIndex != -1) {
			combo.select(currentElementIndex);
		}
	}
	
	private void setupCombo(Combo combo, boolean[] elements, boolean selectedValue) {
		combo.removeAll();
		if(elements.length == 0) {
			combo.setEnabled(false);
			return;
		}
		combo.setEnabled(true);
		int currentElementIndex = -1;
		for(int i=0; i<elements.length; i ++) {
			boolean element = elements[i];
			if(element == selectedValue) {
				currentElementIndex = i;
			}
			combo.add(Boolean.toString(element));
		}
		if(currentElementIndex != -1) {
			combo.select(currentElementIndex);
		}
	}
	
	private void setupCombo(Combo combo, double[] elements, double selectedValue) {
		combo.removeAll();
		if(elements.length == 0) {
			combo.setEnabled(false);
			return;
		}
		combo.setEnabled(true);
		int currentElementIndex = -1;
		for(int i=0; i<elements.length; i ++) {
			double element = elements[i];
			if(element == selectedValue) {
				currentElementIndex = i;
			}
			combo.add(Double.toString(element));
		}
		if(currentElementIndex != -1) {
			combo.select(currentElementIndex);
		}
	}
}
